package com.ruoyi.iot.util;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.http.*;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * @author liuxiaojie
 * @date 2022/9/2 15:56
 */
public class HttpUtils {
    /**
     * 获取token(form参数)
     * @param url
     * @param params
     * @param tokenField
     * @return
     */
    public static String getToken(String url, HashMap<String,String> params,String tokenField){
        String param= HttpUtil.toParams(params);
        String path =url+"?"+param;
        String tokenInfo = HttpUtil.get(path, CharsetUtil.CHARSET_UTF_8);
        JSONObject jsonTokenInfo = JSONUtil.parseObj(tokenInfo);
        return jsonTokenInfo.getStr(tokenField);
    }

    /**
     * get获取数据(form参数)
     * @param url
     * @param params
     * @return
     */
    public static JSONObject getHttpData(String url, HashMap<String,Object> params){
        HttpResponse httpResponse =
                HttpRequest.get(url).setConnectionTimeout(30*1000)
                        .header(Header.ACCEPT_CHARSET, "utf-8")
                        .form(params)
                        .execute();
        if (HttpStatus.HTTP_OK != httpResponse.getStatus()) {
            System.out.println(httpResponse.body());
//            throw new BaseBusinessException(org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR, "请求失败!");
        }
        return JSONUtil.parseObj(httpResponse.body());
    }

    /**
     * get获取数据(form参数)
     * @param url
     * @param params
     * @return
     */
    public static String getHttpDataString(String url, HashMap<String,Object> params){
        HttpResponse httpResponse =
                HttpRequest.get(url).setConnectionTimeout(30*1000)
                        .header(Header.ACCEPT_CHARSET, "utf-8")
                        .form(params)
                        .execute();
        if (HttpStatus.HTTP_OK != httpResponse.getStatus()) {
            System.out.println(httpResponse.body());
//            throw new BaseBusinessException(org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR, "请求失败!");
        }
        return httpResponse.body();
    }

    /**
     * get方法获取数据(url参数)
     * @param path
     * @param param
     * @param tokenname
     * @param token
     * @return
     */
    public static String getUrlHttpDataString(String path, Map<String,Object> param,String tokenname,String token) {
        String params=HttpUtil.toParams(param);
        HttpResponse httpResponse =
                HttpRequest.get(path+"?"+params)
                        .header(tokenname,token).setConnectionTimeout(30*1000)
                        .header(Header.ACCEPT_CHARSET, "utf-8")
                        .execute();
        if (HttpStatus.HTTP_OK != httpResponse.getStatus()) {
            System.out.println(httpResponse.body());
//            throw new BaseBusinessException(org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR, "请求失败!");
        }
        return httpResponse.body();
    }

    public static JSONObject getUrlHttpDataUrlParam(String path,Map<String,Object> param){
        String params=HttpUtil.toParams(param);
        HttpResponse httpResponse =HttpRequest.get(path+"?"+params).header(Header.ACCEPT_CHARSET, "utf-8").execute();
        if (HttpStatus.HTTP_OK != httpResponse.getStatus()) {
            System.out.println(httpResponse.body());
//            throw new BaseBusinessException(org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR, "请求失败!");
        }
        return JSONUtil.parseObj(httpResponse.body());
    }

    /**
     * get方法获取数据(form参数)
     * @param path
     * @param param
     * @param tokenname
     * @param token
     * @return
     */
    public static String getHttpDataString(String path, Map<String,Object> param,String tokenname,String token) {
        HttpResponse httpResponse =
                HttpRequest.get(path).setConnectionTimeout(30*1000)
                        .header(tokenname,token)
                        .header(Header.ACCEPT_CHARSET, "utf-8")
                        .form(param)
                        .execute();
        if (HttpStatus.HTTP_OK != httpResponse.getStatus()) {
            System.out.println(httpResponse.body());
//            throw new BaseBusinessException(org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR, "请求失败!");
        }
        return httpResponse.body();
    }

    /**
     * get方法获取JSON数据(form参数)
     * @param path
     * @param param
     * @param tokenname
     * @param token
     * @return
     */
    public static JSONObject getHttpDataJson(String path, Map<String,Object> param,String tokenname,String token) {
        HttpResponse httpResponse =
                HttpRequest.get(path).setConnectionTimeout(30*1000)
                        .header(tokenname,token)
                        .header(Header.ACCEPT_CHARSET, "utf-8")
                        .form(param)
                        .execute();
        if (HttpStatus.HTTP_OK != httpResponse.getStatus()) {
            System.out.println(httpResponse.body());
//            throw new BaseBusinessException(org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR, "请求失败!");
        }
        return JSONUtil.parseObj(httpResponse.body());
    }

    /**
     * get方法获取JSON数据(form参数)
     * @param path
     * @param param
     * @param tokenname
     * @param token
     * @return
     */
    public static String getHttpDataStringByForm(String path, Map<String,Object> param,String tokenname,String token) {
        HttpResponse httpResponse =
                HttpRequest.get(path).setConnectionTimeout(30*1000)
                        .header(tokenname,token)
                        .header(Header.ACCEPT_CHARSET, "utf-8")
                        .form(param)
                        .execute();
        if (HttpStatus.HTTP_OK != httpResponse.getStatus()) {
            System.out.println(httpResponse.body());
//            throw new BaseBusinessException(org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR, "请求失败!");
        }
        return  httpResponse.body();
    }

    /**
     * post方法获取Json数据(form参数)
     * @param path
     * @param param
     * @return
     */
    public static JSONObject postFormHttpDataJson(String path, Map<String,Object> param) {
        String params=HttpUtil.toParams(param);
        HttpRequest request=HttpRequest.post(path).setConnectionTimeout(30*1000)
                .header(Header.ACCEPT_CHARSET, "utf-8")
                .header(Header.CONTENT_TYPE, ContentType.FORM_URLENCODED.getValue()).form(param);
        HttpResponse httpResponse =request.execute();
        if (HttpStatus.HTTP_OK != httpResponse.getStatus()) {
            System.out.println(httpResponse.body());
//            throw new BaseBusinessException(org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR, "请求失败!");
        }
        return JSONUtil.parseObj(httpResponse.body());
    }

    /**
     * post方法获取数据(form参数)
     * @param path
     * @param param
     * @param tokenname
     * @param token
     * @return
     */
    public static String postHttpData(String path, Map<String,Object> param,String tokenname,String token) {
        String params=HttpUtil.toParams(param);
        HttpRequest request=HttpRequest.post(path+"?"+params)
                .header(tokenname, token).setConnectionTimeout(30*1000)
                .header(Header.ACCEPT_CHARSET, "utf-8")
                .header(Header.CONTENT_TYPE, ContentType.FORM_URLENCODED.getValue());
        HttpResponse httpResponse =request.execute();
        if (HttpStatus.HTTP_OK != httpResponse.getStatus()) {
            System.out.println(httpResponse.body());
//            throw new BaseBusinessException(org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR, "请求失败!");
        }
        return httpResponse.body();
    }

    /**
     * post方法获取Json数据(form参数)
     * @param path
     * @param param
     * @return
     */
    public static JSONObject postHttpDataJson(String path, Map<String,Object> param) {
        String params=HttpUtil.toParams(param);
        HttpRequest request=HttpRequest.post(path+"?"+params).setConnectionTimeout(30*1000)
                .header(Header.ACCEPT_CHARSET, "utf-8")
                .header(Header.CONTENT_TYPE, ContentType.FORM_URLENCODED.getValue());
        HttpResponse httpResponse =request.execute();
        if (HttpStatus.HTTP_OK != httpResponse.getStatus()) {
            System.out.println(httpResponse.body());
//            throw new BaseBusinessException(org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR, "请求失败!");
        }
        return JSONUtil.parseObj(httpResponse.body());
    }
    /**
     * post方法获取Json数据(json参数)
     * @param path
     * @param param
     * @return
     */
    public static JSONObject postDataByJson(String path, Map<String,Object> param) {
        String jsparams=JSONUtil.toJsonStr(param);
        HttpRequest request=HttpRequest.post(path).setConnectionTimeout(30*1000)
                .header(Header.ACCEPT_CHARSET, "utf-8")
                .header(Header.CONTENT_TYPE, ContentType.JSON.getValue()).body(jsparams);
        HttpResponse httpResponse =request.execute();
        if (HttpStatus.HTTP_OK != httpResponse.getStatus()) {
            System.out.println(httpResponse.body());
//            throw new BaseBusinessException(org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR, "请求失败!");
        }
        return JSONUtil.parseObj(httpResponse.body());
    }


    /**
     * post方法获取Json数据(json参数)
     * @param path
     * @param param
     * @return
     */
    public static JSONObject postDataByJsonStr(String path, String param) {
        HttpRequest request=HttpRequest.post(path).setConnectionTimeout(30*1000)
                .header(Header.ACCEPT_CHARSET, "utf-8")
                .header(Header.CONTENT_TYPE, ContentType.JSON.getValue()).body(param);
        HttpResponse httpResponse =request.execute();
        if (HttpStatus.HTTP_OK != httpResponse.getStatus()) {
            System.out.println(httpResponse.body());
//            throw new BaseBusinessException(org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR, "请求失败!");
        }
        return JSONUtil.parseObj(httpResponse.body());
    }



    /**
     * post方法获取Json数据(form参数)
     * @param path
     * @param param
     * @param tokenname
     * @param token
     * @return
     */
    public static JSONObject postHttpDataJson(String path, Map<String,Object> param,String tokenname,String token) {
        String params=HttpUtil.toParams(param);
        HttpRequest request=HttpRequest.post(path+"?"+params)
                .header(tokenname, token).setConnectionTimeout(30*1000)
                .header(Header.ACCEPT_CHARSET, "utf-8")
                .header(Header.CONTENT_TYPE, ContentType.FORM_URLENCODED.getValue());
        HttpResponse httpResponse =request.execute();
        if (HttpStatus.HTTP_OK != httpResponse.getStatus()) {
            System.out.println(httpResponse.body());
//            throw new BaseBusinessException(org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR, "请求失败!");
        }
        return JSONUtil.parseObj(httpResponse.body());
    }

    /**
     * post方法获取Json数据(json参数)
     * @param path
     * @param param
     * @param tokenname
     * @param token
     * @return
     */
    public static JSONObject postHttpDataByJson(String path, Map<String,Object> param,String tokenname,String token) {
        String jsparams=JSONUtil.toJsonStr(param);
        HttpRequest request=HttpRequest.post(path)
                .header(tokenname, token).setConnectionTimeout(30*1000)
                .header(Header.ACCEPT_CHARSET, "utf-8")
                .header(Header.CONTENT_TYPE, ContentType.JSON.getValue()).body(jsparams);
        HttpResponse httpResponse =request.execute();
        if (HttpStatus.HTTP_OK != httpResponse.getStatus()) {
            System.out.println(httpResponse.body());
//            throw new BaseBusinessException(org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR, "请求失败!");
        }
        return JSONUtil.parseObj(httpResponse.body());
    }
}
